/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Usuario;

/**
 *
 * @author Rodrigo
 */
public class UsuarioServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        Usuario user = (Usuario) request.getSession().getAttribute("usuarioLogado");
        if (user == null) {
            String resposta;
                resposta =
            "<div style=\"float: left; position: relative; width: 90px;\">" +
                        "<div>" +
                            "<label>Usuário:</label>" +
                        "</div>" +
                        "<div style=\"margin-top: 10px;\">" +
                            "<label>Senha:</label>" +
                        "</div>" +
                    "</div>" +
                    "<form action=\"LoginServlet\" >" +
                        "<div style=\"float: left; position: relative;\">" +
                            "<div>" +
                                "<input class=\"textinput\" type=\"text\" name=\"user\" size=\"20\"/>" +
                            "</div>" +
                            "<div>" +
                                "<input class=\"textinput\" type=\"password\" name=\"senha\" size=\"20\" style=\"bottom: -7px;\"/>" +
                            "</div>" +
                        "</div>" +
                        "<div style=\"float: left; margin-left: 15px;\" >" +
                            "<button type=\"submit\" class=\"botaologin\"></button>" +
                        "</div>" +
                    "</form>" +
                    "<div style=\"float: left; margin-left: 15px;  margin-top: 15px;\">" +
                        "<label class=\"text\">";
                                String invalido = (String) request.getSession().getAttribute("loginInvalido");
                                if (invalido != null) {
                                    if (invalido.equals("true")) {
                                        resposta += ""+((String) request.getSession().getAttribute("msgErro"));
                                    }
                                }
                       resposta += "</label>" +
                    "</div>" +
                    "<div style=\"float: right; position:relative\">" +
                        "<div><a href=\"cadastro.jsp\">Criar usuário</a></div>" +
                        "<div style=\"margin-top: 10px;\"><a href=\"esquecisenha.jsp\">Esqueci minha senha</a></div>" +
                    "</div>";
                       
               try {
                out.println(resposta);
            } finally {
                out.close();
            }
        } else {
            try {

                String resposta;
                resposta =
                        "<div class=\"user\">"
                        +   "<div style=\"float: left; position: relative;\">"
                        +       "<div>"
                        +           "<label>Bem vindo(a) </label>"
                        +       "</div>"
                        +   "</div>"
                        +   "<form action=\"LogoffServlet\">"
                        +       "<div >"
                        +           "<div style=\"float: left; position: relative; margin-left: 5px;\">"
                        +               "<label class=\"text\" ><font color=\"yellow\">" + user.getLogin() + "</font></label>"
                        +           "</div>"
                        +           "<div style=\"float: left; margin-left: 15px; margin-top: -6px;\">"
                        +               "<button type=\"submit\" class=\"botaologoff\"></button>"
                        +           "</div>"
                        +       "</div>"
                        +   "</form>"
                        +   "<div style=\"float: right; position:relative\">"
                        +       "<div>"
                        +           "<div style=\"margin-top:6px; float:left; margin-bottom: 0px; \" class=\"textos\"> "
                        +               "<img usemap=\"#coin\" id=\"coin\" alt=\"Ouro\" border=\"0\" width=\"16\" height=\"16\" src=\"imgs/coin4.png\" />"
                        +               "<map id=\"coin1\" name=\"coin\">"
                        +                   "<area shape=\"rect\" coords=\"0,0,16,16\"  alt=\"Ouro\" title=\"Ouro\"    />"
                        +               "</map>"
                        +           "</div>"
                        +           "<label style=\"margin-left: 10px; \" > " + user.getOuro() + "</label> "
                        +       "</div>"
                        +       ""
                        +   "</div>"
                        + "</div>";

                out.println(resposta);
            } finally {
                out.close();
            }
        }

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
